package com.os.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.os.crm.mapper.DataTaskMapper;
import com.os.crm.mapper.EnterpriseMapper;
import com.os.crm.mapper.TaskDetailMapper;
import com.os.crm.pojo.DataTask;
import com.os.crm.pojo.Enterprise;
import com.os.crm.pojo.TaskDetail;
import com.os.util.Tools;
import com.os.util.page.PageRequest;
import com.os.util.page.PageResult;
import com.os.util.page.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class DataTaskService {
    @Autowired
    private DataTaskMapper dataTaskMapper;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Autowired
    private TaskDetailMapper taskDetailMapper;
    /**
     * 采用分页插件进行分页
     * @param pageRequest
     * @param map
     * @return
     */
    public PageResult selectByPage(PageRequest pageRequest, Map map) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<DataTask> dataTaskList = dataTaskMapper.selectByParam(map);
        Tools.indexOrder(dataTaskList,"index");
        PageInfo pageInfo = new PageInfo<DataTask>(dataTaskList);
        return PageUtils.getPageResult(pageRequest, pageInfo);
    }
    /**
     * 按ID查询数据采集任务
     * @param id
     * @return
     */
    public DataTask selectById(int id){
        return dataTaskMapper.selectById(id);
    }

    /**
     * 按ID查询数据采集任务
     * @param id
     * @return
     */
    public DataTask selectTaskDetail(int id){
        DataTask dataTask = dataTaskMapper.selectById(id);
        dataTask.setEnterpriseList(dataTaskMapper.selectTaskDetail(id));
        return dataTask;
    }

    /**
     * 新增数据采集任务
     * @param dataTask
     */
    public void insert(DataTask dataTask){
        dataTaskMapper.insertSelective(dataTask);
    }
    /**
     * 修改数据采集任务
     * @param dataTask
     */
    public void update(DataTask dataTask){
        dataTaskMapper.updateByPrimaryKeySelective(dataTask);
    }

    /**
     * 按ID删除数据采集任务
     * @param id
     */
    public void deleteById(int id){
        dataTaskMapper.deleteById(id);
    }

    /**
     * 数据采集任务及执行结果
     * @param dataTask
     */
    public void saveDataTask(DataTask dataTask){
        //保存任务
        dataTaskMapper.insertSelective(dataTask);
        //保存明细
        List<Enterprise> enterpriseList = dataTask.getEnterpriseList();
        int insertCount = 0;
        int updateCount = 0;
        if(!CollectionUtils.isEmpty(enterpriseList)){
            for(int i=0; i<enterpriseList.size(); i++){
                Enterprise enterprise = enterpriseList.get(i);
                enterprise.setBusiness_name(dataTask.getKeyword()==null? dataTask.getBusiness_name():dataTask.getKeyword());
                int insertOrUpdate = enterpriseService.save(enterprise);
                if(insertOrUpdate == EnterpriseService.SAVE_TYPE_INSERT){
                    insertCount++;
                }else{
                    updateCount++;
                }
                TaskDetail taskDetail = new TaskDetail();
                taskDetail.setTask_id(dataTask.getTask_id());
                taskDetail.setEnt_id(enterprise.getEnt_id());
                taskDetail.setCreate_by(dataTask.getCreate_by());
                taskDetail.setUpdate_by(dataTask.getUpdate_by());
                taskDetail.setCreate_time(dataTask.getCreate_time());
                taskDetail.setUpdate_time(dataTask.getUpdate_time());
                taskDetailMapper.insertSelective(taskDetail);
            }
        }
        dataTask.setInsert_count(insertCount);
        dataTask.setUpdate_count(updateCount);
        dataTaskMapper.updateByPrimaryKeySelective(dataTask);
    }


}